﻿Imports System.Web.UI
Imports System.ComponentModel
Imports System.Security.Permissions

<AspNetHostingPermission(SecurityAction.LinkDemand, Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, Level:=AspNetHostingPermissionLevel.Minimal)> _
<DefaultProperty("Text")> _
Public Class FileUpload
    Inherits Control
    Implements IScriptControl

    Private _Height As Unit
    Private _Width As Unit
    Private _CssClass As String
    Private _TabIndex As String
    Private _Enable As Boolean
    Private _MaxUploadFileCount As Integer

    <Category("Layout")> _
    <DefaultValue(GetType(Unit), "")> _
    Public Shadows Property Height As Unit
        Get
            Return _Height
        End Get
        Set(value As Unit)
            _Height = value
        End Set
    End Property

    <Category("Layout")> _
    <DefaultValue(GetType(Unit), "")> _
    Public Shadows Property Width As Unit
        Get
            Return _Width
        End Get
        Set(value As Unit)
            _Width = value
        End Set
    End Property

    <Category("Appearance")> _
    <Localizable(True)> _
    <DefaultValue("")> _
    Public Property Text As String
        Get
            If IsNothing(ViewState("Text")) Then
                Return String.Empty
            Else
                Return ViewState("Text").ToString()
            End If
        End Get
        Set(value As String)
            ViewState("Text") = value
        End Set
    End Property

    <Category("Appearance")> _
    <DefaultValue("")> _
    Public Property CssClass As String
        Get
            Return _CssClass
        End Get
        Set(value As String)
            _CssClass = value
        End Set
    End Property

    <Category("Appearance")> _
    <DefaultValue("")> _
    Public Property TabIndex As String
        Get
            Return _TabIndex
        End Get
        Set(value As String)
            _TabIndex = value
        End Set
    End Property

    <Category("Behavior")> _
    <DefaultValue(True)> _
    Public Property Enable As Boolean
        Get
            Return _Enable
        End Get
        Set(value As Boolean)
            _Enable = value
        End Set
    End Property

    <Category("Data")> _
    <DefaultValue(0)> _
    Public Property MaxUploadFileCount As Integer
        Get
            Return _MaxUploadFileCount
        End Get
        Set(value As Integer)
            _MaxUploadFileCount = value
        End Set
    End Property

    Public Overrides Sub Focus()
        MyBase.Focus()
    End Sub

    Protected Overrides Sub OnPreRender(e As System.EventArgs)

        If Not Me.DesignMode Then
            Dim objScriptManager As ScriptManager = ScriptManager.GetCurrent(Page)
            If Not IsNothing(objScriptManager) Then
                objScriptManager.RegisterScriptControl(Me)
            End If
        End If
        MyBase.OnPreRender(e)
    End Sub

    Protected Overrides Sub Render(writer As System.Web.UI.HtmlTextWriter)

        writer.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID & "_Inputs")
        writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "inline-block")
        writer.AddStyleAttribute(HtmlTextWriterStyle.Position, "relative")
        If Not String.IsNullOrEmpty(_CssClass) Then
            writer.AddAttribute("class", _CssClass)
        End If

        If Not _Width.IsEmpty Then
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, _Width.ToString())
        End If

        If Not _Height.IsEmpty Then
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, _Height.ToString())
        End If
        writer.RenderBeginTag(HtmlTextWriterTag.Span) 'DIV

        writer.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID)
        writer.RenderBeginTag(HtmlTextWriterTag.Span)
        writer.Write(Me.Text)
        writer.RenderEndTag() 'Span

        writer.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID & "_Input_0")
        writer.AddAttribute(HtmlTextWriterAttribute.Name, Me.ClientID & "_Input_0")
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "file")

        If _Enable = False Then
            writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled")
        End If

        If Not String.IsNullOrEmpty(_TabIndex) Then
            writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, _TabIndex)
        End If
        writer.RenderBeginTag(HtmlTextWriterTag.Input)
        writer.RenderEndTag() 'File Count Hidden
        writer.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID & "_hidden_0")
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "hidden")
        writer.AddAttribute(HtmlTextWriterAttribute.Value, _MaxUploadFileCount.ToString())

        writer.RenderBeginTag(HtmlTextWriterTag.Input)
        writer.RenderEndTag() 'Input File


        writer.RenderEndTag() 'Div

        If Not Me.DesignMode Then
            ScriptManager.GetCurrent(Page).RegisterScriptDescriptors(Me)
        End If
        MyBase.Render(writer)
    End Sub

    Protected Overridable Function GetScriptReferences() As IEnumerable(Of ScriptReference)
        Dim reference As ScriptReference = New ScriptReference()
        If Not IsNothing(Page) Then
            reference.Path = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "Tohto.Base.AjaxCustomControl.FileUpload.js")
        End If

        Return New ScriptReference() {reference}
    End Function

    Protected Overridable Function GetScriptDescriptors() As IEnumerable(Of ScriptDescriptor)
        Dim objScriptControlDescriptor As ScriptControlDescriptor = New ScriptControlDescriptor("Tohto.Base.AjaxCustomControl.FileUpload", Me.ClientID)
        objScriptControlDescriptor.AddProperty("text", Me.Text)
        objScriptControlDescriptor.AddProperty("cssClass", Me.CssClass)

        Return New ScriptDescriptor() {objScriptControlDescriptor}
    End Function



    Function IScriptControlGetScriptReferences() As IEnumerable(Of ScriptReference) Implements IScriptControl.GetScriptReferences
        Return GetScriptReferences()
    End Function

    Function IScriptControlGetScriptDescriptors() As IEnumerable(Of ScriptDescriptor) Implements IScriptControl.GetScriptDescriptors
        Return GetScriptDescriptors()
    End Function


End Class
